﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using OgilvyOne.UKNOW.EntityDB;
using OgilvyOne.UKNOW.Entity;
using System.Text.RegularExpressions;

public partial class en_member_become : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string IsUserAvailable(string username)
    {
        string str = string.Empty;
        if (!string.IsNullOrEmpty(username))
        {

            DataTable dt = ContactInfoDB.SelectByUserName(username, "ContactByUserName");
            //从数据库中读取,根据userName 存在 isExists=true
            if (dt != null && dt.Rows.Count > 0)
            {
                str = "{Exist:'1'}";
            }
            else
            {
                str = "{Exist:'0'}";
            }
        }
        return str;
    }

    /// <summary>
    /// 验证用户是否为空
    /// </summary>
    /// <returns></returns>
    private bool ValidateUser()
    {
        bool isvalid = true;
        string username = tbxUserName.Text;
        if (username == hideUserName.Value)
        {
            username = "";
        }
        if (string.IsNullOrEmpty(username))
        {

            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "NameIsNull", @"$(function () {  $('#errorUserName').find('span').empty();   $('#errorUserName').show().append('<span>请输入您的用户名</span>'); });", true);
            isvalid = false;
        }
        
        return isvalid;

    }
    /// <summary>
    /// 验证用户是否存在
    /// </summary>
    /// <returns></returns>
    private bool ExistsUser()
    {
        bool isExist = false;
        string username = tbxUserName.Text; ;
        if (username == hideUserName.Value)
        {
            username = "";
            return false;
        }
        else
        {
            username = tbxUserName.Text;
        }
        if (!string.IsNullOrEmpty(username))
        {
            DataTable dt = ContactInfoDB.SelectByUserName(username, "ContactByUserName");
            //从数据库中读取,根据userName 存在 isExists=true
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
        }
        return isExist;

    }

    /// <summary>
    /// 验证用户是否存在
    /// </summary>
    /// <returns></returns>
    private bool ExistsMail()
    {
        bool isExist = false;

        string mail = tbxUserMail.Text;

        if (mail == hideMail.Value)
        {
            mail = "";
            return false;
        }
        else
        {
            mail = tbxUserMail.Text;
        }
        if (!string.IsNullOrEmpty(mail))
        {
            DataTable dt = ContactInfoDB.SelectByMail(mail, "ContactByMail");
            //从数据库中读取,根据userName 存在 isExists=true
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
        }
        return isExist;

    }

    /// <summary>
    /// 邮件验证
    /// </summary>
    /// <returns></returns>
    private bool ValidateEmail()
    {
        bool isvalid = true;
        string email = tbxUserMail.Text;
        if (email == hideMail.Value)
        {
            email = "";
        }
        if (!string.IsNullOrEmpty(email)&&(email!=hideMail.Value))
        {
            string strRegex = @"^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$";
            Regex re = new Regex(strRegex);
            if (!re.IsMatch(email))
            {
                isvalid = false;
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "MailError", @"$(function () {  $('#errorEmail').find('span').empty();   $('#errorEmail').show().append('<span>请输入有效的电子邮件地址</span>'); });", true);
            }


        }
        else
        {
            isvalid = false;
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "MailIsNull", @"$(function () { $('#errorEmail').find('span').empty();  $('#errorEmail').show().append('<span>请输入电子邮件地址</span>'); });", true);
        }

        return isvalid;
    }

    /// <summary>
    /// 手机及电话号码验证
    /// </summary>
    /// <returns></returns>
    private bool ValidateMobile()
    {
        bool isvalid = true;
        
        string mobile = tbxPhone.Text;
        if (mobile == hidePhoneNum.Value)
        {
            mobile = "";
        }
        if (!string.IsNullOrEmpty(mobile) && (mobile != hidePhoneNum.Value))
        {
            string strRegex = @"^1[3|4|5|8][0-9]\d{4,8}$";
            string strTel = @"^\d{3,4}-?\d{7,9}$";
            Regex re = new Regex(strRegex);
            Regex reTel = new Regex(strTel);
            if ((re.IsMatch(mobile)) || (reTel.IsMatch(mobile)))
            {
                isvalid = true;
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "PhoneError", @"$(function () {$('#errorMobile').find('span').empty(); $('#errorMobile').show().append('<span>无效的手机号码,请重试</span>'); });", true);
                isvalid = false;
            }

        }
        else
        {
            tbxPhone.Text = "";
        }

        return isvalid;
    }

    /// <summary>
    /// userType:1  中文注册
    /// userType:0  英文注册
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbSubmit_Click(object sender, EventArgs e)
    {
        string errMsg = string.Empty;
        string userType = "1";
        bool isOK = true;
        if (!ValidateUser())
        {
            isOK = false;
            
        }
        
//        if (ExistsUser())
//        {
//            isOK = false;
//            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "UserExits", @"$(function () 
//            {$('#errorUserName').find('span').empty();
//            $('#errorUserName').show().append('<span>该用户已经存在</span>');
//           
//            });", true);
            
//            return;
//        }

//        if (ExistsMail())
//        {
//            isOK = false;
//            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "MailExits", @"$(function () 
//            {$('#errorEmail').find('span').empty();
//            $('#errorEmail').show().append('<span>该电子邮件地址已经存在</span>');
//            });", true);
//            return;
            
//        }
      
        if (!ValidateEmail())
        {
            isOK = false;
        }
        else
        { 
            
        }
        if (!ValidateMobile())
        {
            isOK = false;
        }
        if (isOK)
        {
            ContactInfo contactInfo = GetContactInfo();
            int i = ContactInfoDB.Insert(contactInfo, "ContactInsert",userType);
            if (i > 0)
            {
                //注册成功
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", @"$(function () {$('input:text').val('');alert('谢谢. 您的联系信息已经被成功提交')});", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('注册失败,请重试')", true);
            }
        }
        else
        {
            return;
        }
       
    }


    public ContactInfo GetContactInfo()
    {
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.UserName = tbxUserName.Text;
        contactInfo.Mobile = tbxPhone.Text;
        contactInfo.Email = tbxUserMail.Text;
        contactInfo.Nature = (tbxNature.Text==hideNature.Value)?"":tbxNature.Text;
        return contactInfo;
    }
         
   
}
